package com.lwt.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lwt.model.District;
import com.lwt.model.House;
import com.lwt.model.Street;
import com.lwt.model.Types;
import com.lwt.service.HouseService;
import com.lwt.util.Condition;
import com.lwt.util.Page;
import com.lwt.util.Result;

@RestController
@RequestMapping("/house")
public class HouseController {
	/**
	 * 注入业务服务
	 */
	@Autowired
	private HouseService houseservice;

	/**
	 * 模糊查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "/querylike.do")
	public Page queryLikeHouse(int pageSize, int pageNum, Condition cond) {
		System.out.println("模糊查询");
		System.out.println(cond);
		Map<String, Object> houseinfo = new HashMap<String, Object>();
		if (!"".equals(cond.getPrice()) && cond.getPrice() != null) {
			Integer minPrice = Integer.valueOf(cond.getPrice().split("-")[0]);
			Integer maxPrice = Integer.valueOf(cond.getPrice().split("-")[1]);
			houseinfo.put("minPrice", minPrice);
			houseinfo.put("maxPrice", maxPrice);
		}
		if (!"".equals(cond.getFloorage()) && cond.getFloorage() != null) {
			Integer minFloorage = Integer.valueOf(cond.getFloorage().split("-")[0]);
			Integer maxFloorage = Integer.valueOf(cond.getFloorage().split("-")[1]);
			houseinfo.put("minFloorage", minFloorage);
			houseinfo.put("maxFloorage", maxFloorage);
		}
		houseinfo.put("pageSize", pageSize);
		houseinfo.put("pageNum", pageNum);
		houseinfo.put("district_id",cond.getDistrict_id());
		houseinfo.put("type_id",cond.getType_id());
		houseinfo.put("title",cond.getTitle());
		Page page = houseservice.getLikeHouse(houseinfo);
		return page;
	}

	/**
	 * 获取租房信息详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/details.do")
	public ModelAndView details(HttpServletRequest request, House house) {
		Map<String, Object> m = houseservice.showHouseById(house);
		Map<String,Object> rsMap = new HashMap<String,Object>();
		rsMap.put("hdetails", m);
		System.out.println("进入编辑页面"+m.toString());
		return new ModelAndView("housedetail",rsMap);
	}

	/**
	 * 删除租房信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/delete.do")
	public Result delete(House house) {
		houseservice.deleteHouse(house);
		return new Result();
	}

	/**
	 * 获取所有房型
	 * 
	 * @return
	 */
	@RequestMapping(value = "/allTypes.do")
	public String allTypes(HttpServletRequest request) {
		List<Types> list = houseservice.showAllTypes();
		request.getSession().setAttribute("typelist", list);
		return "success";
	}

	/**
	 * 根据区域id获取区域下所有街道
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/allStreetByd.do")
	public byte[] allStreetByd(Street street) throws UnsupportedEncodingException {
		List<Street> list = houseservice.showAllStreetByDid(street);
		String msgs = "";
		for (Street s : list) {
			msgs += "<option value='" + s.getId() + "'>" + s.getName() + "</option>";
		}
		return msgs.getBytes("utf-8");
	}

	/**
	 * 增加租房信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add.do")
	public Result addHouse(House house) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		house.setPubdate(sdf.format(new Date()));
		house.setUser_id(1);
		System.out.println(house);
		houseservice.addHouse(house);
		return new Result();
	}

	/**
	 * 获取所有区域
	 * 
	 * @return
	 */
	@RequestMapping(value = "/allDistrict.do")
	public Result allDistrict(HttpServletRequest request) {
		List<District> list = houseservice.showAllDistrict();
		request.getSession().setAttribute("districtlist", list);
		System.out.println("获取所有区域");
		return new Result();
	}

	/**
	 * 准备更新租房信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toupdate.do")
	public ModelAndView toupdate(HttpServletRequest request, House house) {
		Map<String, Object> m = houseservice.showHouseById(house);
		 Map<String,Object> rsMap = new HashMap<String,Object>();
		 rsMap.put("data", m);
		System.out.println("进入编辑页面"+m.toString());
		return new ModelAndView("houseedit",rsMap);
	}

	/**
	 * 更新租房信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/update.do")
	public Result update(House house) {
		boolean a = houseservice.updateHouse(house);
		System.out.println("编辑提交"+a);
		return new Result();
	}

}
